package com.lzy.crm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lzy.crm.base.mybatis.BaseMapper;
import com.lzy.crm.base.mybatis.Example;
import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.service.BaseServiceImpl;
import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.base.util.RedisHelper;
import com.lzy.crm.dao.CardFlowMapper;
import com.lzy.crm.dao.PpUserMapper;
import com.lzy.crm.dao.criteria.PpUserCriteria;
import com.lzy.crm.domain.CardFlow;
import com.lzy.crm.domain.PpUser;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.PpUserService;
import com.lzy.crm.util.PpUserConstants;
import com.lzy.crm.util.UserConstants;
import com.lzy.crm.vo.AgentUser;
import com.lzy.crm.vo.PpUserVo;

@Service("ppUserService")
public class PpUserServiceImpl extends BaseServiceImpl<PpUser,PpUserCriteria> implements PpUserService {

	@Resource
	private PpUserMapper pUserMapper;
	
	@Resource
	private CardFlowMapper cardFlowMapper;
	
	@Override
	public BaseMapper<PpUser, PpUserCriteria> getBaseMapper() {
		return pUserMapper;
	}

	/**
	 * 游戏用户登录
	 * @param uid
	 * @param type
	 * @param passwd
	 * @return
	 */
	public PpUser getPpUserByUnionId(String unionId ){
		Example<PpUserCriteria> example = new Example<PpUserCriteria>(PpUserCriteria.class);
		PpUserCriteria puserCriteria = example.createCriteria();
		puserCriteria.andUnionidEqualTo(unionId);
		return pUserMapper.getObjectByExample(example);
	}
	
	
	
	/**
	 * 游戏用户登录
	 * @param uid
	 * @param type
	 * @param passwd
	 * @return
	 */
	public PpUser getPpUserByopenid(String openid){
		Example<PpUserCriteria> example = new Example<PpUserCriteria>(PpUserCriteria.class);
		PpUserCriteria puserCriteria = example.createCriteria();
		puserCriteria.andOpenidEqualTo(openid);
		return pUserMapper.getObjectByExample(example);
	}
	
	
	/**
	 * 通过邀请码查找
	 * @param uid
	 * @param type
	 * @param passwd
	 * @return
	 */
	public List<PpUser> getPpUserByCode(String code){
		Example<PpUserCriteria> example = new Example<PpUserCriteria>(PpUserCriteria.class);
		PpUserCriteria puserCriteria = example.createCriteria();
		puserCriteria.andCodeEqualTo(code);
		return pUserMapper.getListByExample(example);
	}
	
	/**
	 * 游戏用户登录
	 * @param uid
	 * @param type
	 * @param passwd
	 * @return
	 */
	public PpUser getPpUser(String uid ){
		Example<PpUserCriteria> example = new Example<PpUserCriteria>(PpUserCriteria.class);
		PpUserCriteria puserCriteria = example.createCriteria();
		puserCriteria.andUidEqualTo(uid);
		return pUserMapper.getObjectByExample(example);
	}
	
	
	/**
	 * 查询用户登录用户信息
	 * @param phone
	 * @return
	 */
	public List<PpUser> getPpUserByPhone(String phone){
		return pUserMapper.getPpUserListByPhone(phone);
	}
	
	
	
	/**
	 * 我的推广员查询
	 * @param userId
	 * @param phone
	 * @param pageQuery
	 * @return
	 */
	public List<PpUserVo> getNextUserList(Integer uid,String code,String phone,String nxuid,String realname,String user_level,PageQuery pageQuery){
		
		List<PpUserVo> pUserVoList = new ArrayList<PpUserVo>();
		List<PpUser>  pUserList = pUserMapper.getNextUserList(uid,code, phone, nxuid, realname, user_level, pageQuery);
		for (PpUser pUser : pUserList) {
			PpUserVo pUserVo = JsonUtil.fromJson(JsonUtil.toJson(pUser), PpUserVo.class);
			// 组装所需列
			Integer  parent_nub = pUserMapper.getNextUserCount(pUser.getUid(), null);
			Integer  parent_vip_nub = pUserMapper.getNextUserCount(pUser.getUid(), PpUserConstants.USER_TYPE_AGENT+"");
			pUserVo.setParent_nub(parent_nub);
			pUserVo.setParent_vip_nub(parent_vip_nub);
			pUserVoList.add(pUserVo);
		}
		return pUserVoList;
	}
	
	
	/**
	 * 玩家查询
	 * @param userId
	 * @param phone
	 * @param pageQuery
	 * @return
	 */
	public List<PpUserVo> getUserList(String code,String phone,String uid,String realname,String user_level,PageQuery pageQuery){

		List<PpUserVo> pUserVoList = new ArrayList<PpUserVo>();
		List<PpUser>  pUserList = pUserMapper.getUserList(code, phone, uid, realname, user_level, pageQuery);
		for (PpUser pUser : pUserList) {
			PpUserVo pUserVo = JsonUtil.fromJson(JsonUtil.toJson(pUser), PpUserVo.class);
			// 组装所需列
			// 组装所需列
			Integer  parent_nub = pUserMapper.getNextUserCount(pUser.getUid(), null);
			Integer  parent_vip_nub = pUserMapper.getNextUserCount(pUser.getUid(), PpUserConstants.USER_TYPE_AGENT+"");
			pUserVo.setParent_nub(parent_nub);
			pUserVo.setParent_vip_nub(parent_vip_nub);
			pUserVoList.add(pUserVo);
		}
		return pUserVoList;
		
	}
	
	
	/**
	 *  发送房卡方法
	 * @param lessUser
	 * @param gameId
	 * @param totalCard
	 * @return
	 */
	public String newBuyCard(PpUser lessUser ,Integer gameId ,Integer totalCard){
		
		String resultCode ="01";
		Integer userTotalCard = 0;  //玩家总卡数
		PpUser puser = pUserMapper.getById(lessUser.getUid());
		PpUser addpuser = pUserMapper.getById(gameId);
		if(puser == null){
			resultCode="03";  // 代理信息有误
			return resultCode;
		}
		if(addpuser==null){
			resultCode="04"; // 玩家信息有误
			return resultCode;
		}
		// 买的总卡数 包括赠送的和购买的卡数
		userTotalCard= puser.getRoomcard();
		if(userTotalCard>= totalCard){
			// 卖家剩余卡数
			Integer surplusCard=userTotalCard-totalCard;
			// 扣除代理房卡
			puser.setRoomcard(surplusCard);
			pUserMapper.updateByPrimaryKeySelective(puser);
			
			// 给玩家加房卡
			addpuser.setRoomcard(addpuser.getRoomcard()+totalCard);
			pUserMapper.updateByPrimaryKeySelective(addpuser);
			
			
			// 更新redis 
			Map<String, String> map = new HashMap<String, String>();
			map.put("unionid", puser.getUnionid());
			map.put("openid", puser.getOpenid());
			map.put("headimgurl", puser.getHeadImgUrl());
			map.put("access_token", puser.getToken());
			map.put("name", puser.getNickname());
			map.put("province", puser.getAddress());
			map.put("country", puser.getAddress());
			map.put("cash_num", puser.getRoomcard().toString());
			map.put("city", puser.getAddress());
			map.put("sex", puser.getSex().toString());
			map.put("refresh_token", puser.getRefreshToken());

			RedisHelper.hmset("account:" + puser.getUid() + ":data", map);
			
			Map<String, String> addmap = new HashMap<String, String>();
			addmap.put("unionid", addpuser.getUnionid());
			addmap.put("openid", addpuser.getOpenid());
			addmap.put("headimgurl", addpuser.getHeadImgUrl());
			addmap.put("access_token", addpuser.getToken());
			addmap.put("name", addpuser.getNickname());
			addmap.put("province", addpuser.getAddress());
			addmap.put("country", addpuser.getAddress());
			addmap.put("cash_num", addpuser.getRoomcard().toString());
			addmap.put("city", addpuser.getAddress());
			addmap.put("sex", addpuser.getSex().toString());
			addmap.put("refresh_token", addpuser.getRefreshToken());

			RedisHelper.hmset("account:" + addpuser.getUid() + ":data", addmap);
			
			
			// 生成记录
			/*CardFlow cardFlow =new CardFlow();
			cardFlow.setAId(puser.getUid());
			cardFlow.setAName(puser.getNickname());
			cardFlow.setACard(surplusCard);
			cardFlow.setBId(addpuser.getUid());
			cardFlow.setBName(addpuser.getNickname());
			cardFlow.setCard(addpuser.getRoomcard());
			cardFlow.setAwardCard(0);
			cardFlow.setBeforeCard(addpuser.getRoomcard());
			cardFlow.setAfterCard(addpuser.getRoomcard()+totalCard);
			cardFlow.setFunc(0);
			cardFlow.setRemark(addpuser.getNickname());
			cardFlow.setType(UserConstants.CARD_TYPE_PLAYER);
			cardFlow.setCreateTime(new Date());
			cardFlowMapper.insertSelective(cardFlow);*/
		}else{
			// 返回错误，卖出去的卡 大于代理商能卖的卡数
			resultCode="02";
		}
		return resultCode;
	}
	
	
}
